//
//  OnlineOrganizationalLifeViewController.m
//  ShangHaiHuDongApp
//
//  Created by apple on 17/9/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "OnlineOrganizationalLifeViewController.h"
#import "OnlineOrganizationalLifeTableViewCell.h"
#import "OnlineOrganizationalLifeModel.h"
#import "OnlineOLDetailViewController.h"
#define NORMALCELL @"OnlineOrganizationalLifeTableViewCell"
@interface OnlineOrganizationalLifeViewController ()<UITableViewDelegate , UITableViewDataSource>
@property (nonatomic , strong) UITableView * mainTableView;

@property (nonatomic , strong) NSMutableArray<OnlineOrganizationalLifeModel *> * currentArray;
@property (nonatomic , strong) NSMutableArray<OnlineOrganizationalLifeModel *> * laterArray;
/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
@end

@implementation OnlineOrganizationalLifeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(option_endActivityWithNotification:) name:@"ENDLIFEACT" object:nil];
    self.titlelabel.text = @"组织生活";
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.mainTableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.mainTableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    
    [self.mainTableView.mj_header beginRefreshing];
    [self ybInitInterface];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ENDLIFEACT" object:nil];
}


-(void)option_endActivityWithNotification:(NSNotification *)notification{
    [self.mainTableView.mj_header beginRefreshing];
}

-(void)ybInitInterface{
    [self.view addSubview:self.mainTableView];
}


#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
   
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    
    NSDictionary *paramer  = @{@"BIG_CLASS": @"1",
                               @"SMALL_CLASS": @"",
                               @"PAGE": page,
                               @"COUNT": @"10"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActList4OrgLifelist] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *current = [object objectForKey:@"ORGLIFE_NOT_OVER"];
            weakSelf.currentArray = [[NSArray yy_modelArrayWithClass:[OnlineOrganizationalLifeModel class] json:current] mutableCopy];
            for (OnlineOrganizationalLifeModel * currentModel in weakSelf.currentArray) {
                [currentModel calculateCellTitleSize];
            }
            NSArray *over = [object objectForKey:@"ORGLIFE_OVER"];
            weakSelf.laterArray = [[NSArray yy_modelArrayWithClass:[OnlineOrganizationalLifeModel class] json:over] mutableCopy];
            for (OnlineOrganizationalLifeModel * laterModel in weakSelf.laterArray) {
                [laterModel calculateCellTitleSize];
            }
            
            [weakSelf.mainTableView reloadData];
            if (weakSelf.currentArray.count == 0 && weakSelf.laterArray.count == 0  ) {
                [LYEmptyManager showNoDataWithView:weakSelf.mainTableView];
                
            }
            
        }
        
        [weakSelf.mainTableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.mainTableView.mj_header endRefreshing];
        if (weakSelf.currentArray.count == 0 && weakSelf.laterArray.count == 0) {
            [LYEmptyManager showLoadFailWithView:weakSelf.mainTableView managerBlock:^{
                [weakSelf.mainTableView.mj_header beginRefreshing];
            }];
        }else{
            [YJProgressHUD showMsgWithoutView:@"请求失败"];
        }
        
    }];
    
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    
    NSDictionary *paramer  = @{@"BIG_CLASS": @"1",
                               @"SMALL_CLASS": @"",
                               @"PAGE": page,
                               @"COUNT": @"10"};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActList4OrgLifelist] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *over = [object objectForKey:@"ORGLIFE_OVER"];
            [weakSelf.laterArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[OnlineOrganizationalLifeModel class] json:over]];
        [weakSelf.mainTableView reloadData];
        }
        [weakSelf.mainTableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.mainTableView.mj_footer endRefreshing];
    }];
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == (self.currentArray.count - 1)) {
            return ItemHeight * 220;
        }
        else{
            return ItemHeight * 236;
        }
    }
    else{
        if (indexPath.row == (self.laterArray.count - 1)) {
            return ItemHeight * 220;
        }
        else{
            return ItemHeight * 236;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        if (self.currentArray.count == 0) {
            return 0.01;
        }
        else{
            return ItemHeight * 70;
        }
    }
    else{
        if (self.laterArray.count == 0) {
            return 0.01;
        }
        else{
            return ItemHeight * 70;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.currentArray.count;
    }
    else{
        return self.laterArray.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OnlineOrganizationalLifeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NORMALCELL forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        cell.model = self.currentArray[indexPath.row];
        if (indexPath.row == (self.currentArray.count - 1)) {
            cell.bottomView.hidden = YES;
        }
        else{
            cell.bottomView.hidden = NO;
        }
    }
    else{
        cell.model = self.laterArray[indexPath.row];
        if (indexPath.row == (self.laterArray.count - 1)) {
            cell.bottomView.hidden = YES;
        }
        else{
            cell.bottomView.hidden = NO;
        }
    }
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[NSString stringWithFormat:@"header%ld" , section]];
    if (!header) {
        header = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:[NSString stringWithFormat:@"header%ld" , section]];
        UILabel * theTitleLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 29, ItemHeight * 22, [UIScreen zj_screenWith] * 0.75, ItemHeight * 36) font:14 textColor:[UIColor themeColor] text:@""];
        if (section == 0) {
            theTitleLabel.text = @"正在进行的线上组织生活";
            theTitleLabel.textColor = [UIColor themeColor];
        }
        else{
            theTitleLabel.text = @"往期线上组织生活";
            theTitleLabel.textColor = [UIColor textGrayColor];
        }
        [header.contentView addSubview:theTitleLabel];
    }
    if (section == 0) {
        if (self.currentArray.count == 0) {
            header.hidden = YES;
        }
        else{
            header.hidden = NO;
        }
    }
    else{
        if (self.laterArray.count == 0) {
            header.hidden = YES;
        }
        else{
            header.hidden = NO;
        }
    }
    return header;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf = self;
    if (indexPath.section == 0) {
        OnlineOLDetailViewController *vc = [OnlineOLDetailViewController new];
        vc.model = self.currentArray[indexPath.row];
        vc.applyBlock = ^{
            [weakSelf.mainTableView.mj_header beginRefreshing];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
    else{
        OnlineOLDetailViewController *vc = [OnlineOLDetailViewController new];
        vc.model = self.laterArray[indexPath.row];
        vc.applyBlock = ^{
            [weakSelf.mainTableView.mj_header beginRefreshing];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }

}


-(UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - LP_TopHeight) style:UITableViewStyleGrouped];
        [_mainTableView registerClass:[OnlineOrganizationalLifeTableViewCell class] forCellReuseIdentifier:NORMALCELL];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.backgroundColor = [UIColor themeGrayColor];
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _mainTableView;
}


-(NSMutableArray *)currentArray
{
    if (!_currentArray) {
        _currentArray = [NSMutableArray array];
    }
    return _currentArray;
}

-(NSMutableArray *)laterArray
{
    if (!_laterArray) {
        _laterArray = [NSMutableArray array];
    }
    return _laterArray;
}
- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}


@end
